import { reactive, markRaw, ref } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { BaseTableProps } from '@/components/BaseTable/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { IModelListParams } from '@/apis/pcf/myModel/model/modelManage'
import { getModelList, getTagList, getRecycleCount } from '@/apis/pcf/myModel/modelList'
import { getDqrList } from '@/apis/pcf/myModel/dqr'

import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'
import { useRoute } from 'vue-router'
import type { CTableProps } from '@/components/CTable'

export const useList = () => {
  const route = useRoute()
  const { modelNo } = route.params
  localStorage.setItem('dqrModelNo', modelNo as string)
  // 搜索字段及其他
  const searchForm = reactive({
    nameOrCode: undefined,
    lifeCycleStage: undefined,
    processType: undefined,
    dqrStatus: undefined,
    nodeName: undefined
  })
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '生命周期阶段',
      field: 'lifeCycleStage',
      type: 'select',
      fixedOption: 'ProcessStageOption'
    },
    {
      name: '数据类型',
      field: 'processType',
      type: 'select',
      fixedOption: 'dqrTypeOptions'
    },
    {
      name: '数据名称',
      field: 'nodeName',
      type: 'input'
    },
    {
      name: '评分状态',
      field: 'dqrStatus',
      type: 'select',
      fixedOption: 'dqrStatusOptions'
    }
  ])

  // 表格配置
  const tableOptions: CTableProps = {
    loadData,
    selection: true,
    hasCollapse: false,
    border: true,
    extraTableOption: {
      checkboxConfig: {
        checkField: 'checked', // 避免多数据时全选卡顿
        trigger: 'default'
      }
      // scrollY: { enabled: true, gt: 50, oSize: 30, rSize: 50 }, // gt50数量超过50开启虚拟滚动
      // maxHeight: '100%',
      // rowConfig: { height: 56 }
    },
    rowKey: 'nodeNo'
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '生命周期阶段',
      field: 'lifeCycleStage',
      slot: 'lifeCycleStage',
      width: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '数据类型',
      field: 'processType',
      width: 100,
      slot: 'processType',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '数据名称',
      field: 'nodeName',
      slot: 'dataSlot',
      minWidth: 140,
      columnSettingConfig: {
        enabled: true
      }
    },

    {
      title: '初级数据评分',
      children: [
        {
          title: '准确性（P）',
          field: 'P',
          slot: 'P',
          width: 110,
          columnSettingConfig: {
            enabled: true
          }
        },
        {
          title: '时间代表性（TiR）',
          field: 'TiR',
          slot: 'TiR',
          width: 110,
          columnSettingConfig: {
            enabled: true
          }
        },
        {
          title: '技术代表性（TeR）',
          field: 'TeR',
          slot: 'TeR',
          width: 110,
          columnSettingConfig: {
            enabled: true
          }
        },
        {
          title: '地域代表性（GeR）',
          field: 'GeR',
          slot: 'GeR',
          width: 110,
          columnSettingConfig: {
            enabled: true
          }
        }
      ]
    },
    {
      title: '排放因子',
      field: 'factorName',
      overflow: false,
      width: 140,
      slot: 'factor',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '次级数据评分',
      children: [
        {
          title: '时间代表性（TiR）',
          field: 'TiR1',
          slot: 'factorTiR',
          width: 110,
          columnSettingConfig: {
            enabled: true
          }
        },
        {
          title: '技术代表性（TeR）',
          field: 'TeR1',
          slot: 'factorTeR',
          width: 110,
          columnSettingConfig: {
            enabled: true
          }
        },
        {
          title: '地域代表性（GeR）',
          field: 'GeR1',
          slot: 'factorGeR',
          width: 110,
          columnSettingConfig: {
            enabled: true
          }
        }
      ]
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 70,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  function loadData(parameter: BasicPagingListParams) {
    const params = handleParams()
    Object.assign(params, parameter)

    return getDqrList(params)
  }
  const handleParams = (): any => {
    let params: any = {
      modelNo: modelNo,
      lifeCycleStage: searchForm.lifeCycleStage,
      processType: searchForm.processType,
      nodeName: searchForm.nodeName,
      dqrStatus: searchForm.dqrStatus
    }
    params = removeUndefinedParams(params)
    return params
  }

  return {
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting,
    searchOptions,
    modelNo
  }
}
